package com.ruoyi.web.controller.market.special;

import cn.hutool.core.util.StrUtil;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.db.domain.SpecialCostomInfo;
import com.ruoyi.db.domain.SpecialCostomItemInfo;
import com.ruoyi.db.domain.SpecialOperatorLog;
import com.ruoyi.market.enums.InfoTypeEnum;
import com.ruoyi.market.enums.ManagerTypeEnum;
import com.ruoyi.market.enums.PriceTypeEnum;
import com.ruoyi.market.enums.SpecialEnum;
import com.ruoyi.market.http.service.MarketApiService;
import com.ruoyi.special.service.*;
import com.ruoyi.web.service.ISpecialCostomInfoCommonService;
import com.ruoyi.web.wapper.SpecialCostomInfoWapper;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * 特殊渠道信息Controller
 *
 * @author ruoyi
 * @date 2022-08-15
 */
@Controller
@RequestMapping("/special/info")
public class SpecialCostomInfoController extends BaseController {
    private String prefix = "special/info";

    @Autowired
    private ISpecialCostomInfoService specialCostomInfoService;

    @Autowired
    ISpecialCostomInfoCommonService specialCostomInfoCommonService;

    @Autowired
    ISpecialDataInfoService specialDataInfoService;
    @Autowired
    private ISpecialCostomManagerInfoService specialCostomManagerInfoService;
    @Autowired
    private ISpecialCostomUserInfoService specialCostomUserInfoService;
    @Autowired
    private ISpecialCostomItemInfoService specialCostomItemInfoService;
    @Autowired
    MarketApiService marketApiService;

    @Autowired
    ISpecialOperatorLogService specialOperatorLogService;

    @RequiresPermissions("special:info:view")
    @GetMapping()
    public String info(ModelMap modelMap) {

        List<Map<String, Object>> managerInfoList = specialCostomManagerInfoService.getManagerInfoList(ManagerTypeEnum.SPECIAl.getValue());
        modelMap.put("managerInfoList", managerInfoList);

        List<Map<String, Object>> infoTypeList = InfoTypeEnum.getMapList();
        modelMap.put("infoTypeList", infoTypeList);

        return prefix + "/info";
    }

    /**
     * 查询特殊渠道信息列表
     */
    @RequiresPermissions("special:info:view")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(SpecialCostomInfo specialCostomInfo) {
        startPage();
        List<Map<String, Object>> mapList = specialCostomInfoService.selectSpecialCostomInfoListMap(specialCostomInfo);
        mapList = new SpecialCostomInfoWapper(mapList).wrap();
        return getDataTable(mapList);
    }

    @RequiresPermissions("special:info:mine")
    @GetMapping("/mine")
    public String mine() {
        return prefix + "/mine";
    }

    /**
     * 我的机构
     */
    @RequiresPermissions("special:info:mine")
    @PostMapping("/mineList")
    @ResponseBody
    public TableDataInfo mineList(SpecialCostomInfo specialCostomInfo) {

        SysUser sysUser = getSysUser();
        if (!sysUser.isAdmin()) {
            specialCostomInfo.setOwnerUserId(sysUser.getUserId());
        }

        startPage();
        List<Map<String, Object>> mapList = specialCostomInfoService.selectSpecialCostomInfoMineListMap(specialCostomInfo);
        mapList = new SpecialCostomInfoWapper(mapList).wrap();
        return getDataTable(mapList);
    }


    /**
     * 新增特殊渠道信息
     */
    @GetMapping("/add")
    public String add(ModelMap modelMap) {
        List<Map<String, Object>> managerInfoList = specialCostomManagerInfoService.getManagerInfoList(ManagerTypeEnum.SPECIAl.getValue());
        modelMap.put("managerInfoList", managerInfoList);

        List<Map<String, Object>> infoTypeList = InfoTypeEnum.getMapList();
        modelMap.put("infoTypeList", infoTypeList);

        modelMap.put("priceTypeList", PriceTypeEnum.getMapList());

        return prefix + "/add";
    }

    /**
     * 新增保存特殊渠道信息
     */
    @RequiresPermissions("special:info:add")
    @Log(title = "特殊渠道信息", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(SpecialCostomInfo specialCostomInfo) {
        int result = specialCostomInfoCommonService.insertNewEntity(specialCostomInfo);
        return toAjax(result);
    }

    /**
     * 修改特殊渠道信息
     */
    @RequiresPermissions("special:info:edit")
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") String id, ModelMap mmap) {

        List<Map<String, Object>> managerInfoList = specialCostomManagerInfoService.getManagerInfoList(ManagerTypeEnum.SPECIAl.getValue());
        mmap.put("managerInfoList", managerInfoList);

        SpecialCostomInfo specialCostomInfo = specialCostomInfoService.selectSpecialCostomInfoById(id);
        mmap.put("specialCostomInfo", specialCostomInfo);
        return prefix + "/edit";
    }

    /**
     * 修改保存特殊渠道信息
     */
    @RequiresPermissions("special:info:edit")
    @Log(title = "特殊渠道信息", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(SpecialCostomInfo specialCostomInfo) {
        return toAjax(specialCostomInfoService.updateSpecialCostomInfo(specialCostomInfo));
    }

    @RequiresPermissions("special:info:change")
    @PostMapping("/changeStatus")
    @ResponseBody
    public AjaxResult changeStatus(SpecialCostomInfo specialCostomInfo) {
        SpecialOperatorLog specialOperatorLog = new SpecialOperatorLog();
        specialOperatorLog.setOperatorName(getSysUser().getUserName());
        specialOperatorLog.setOperationType(specialCostomInfo.getStatus());
        specialOperatorLog.setOperatorId(getSysUser().getUserId());
        specialOperatorLog.setInfoId(specialCostomInfo.getId());
        specialOperatorLog.setType(0);
        specialOperatorLogService.insertSpecialOperatorLog(specialOperatorLog);
        return toAjax(specialCostomInfoService.updateSpecialCostomInfo(specialCostomInfo));
    }

    /**
     * 删除特殊渠道信息
     */
    @RequiresPermissions("special:info:remove")
    @Log(title = "特殊渠道信息", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids) {
        return toAjax(specialCostomInfoService.deleteSpecialCostomInfoByIds(ids));
    }

    /**
     * 修改特殊渠道信息
     */
    @RequiresPermissions("special:info:manualPush")
    @GetMapping("/manualPush/{id}")
    public String manualPush(@PathVariable("id") String id, ModelMap mmap) {

        SpecialCostomInfo specialCostomInfo = specialCostomInfoService.selectSpecialCostomInfoById(id);
        mmap.put("specialCostomInfo", specialCostomInfo);

        // 如果是crm,则查询在线人数
        Integer onlineNumber = 0;
        if (StrUtil.equals("CRM", specialCostomInfo.getInfoType())) {
            onlineNumber = specialCostomUserInfoService.countOnlineUserNumber(specialCostomInfo.getId());
        }
        mmap.put("onlineNumber", onlineNumber);

        SpecialCostomItemInfo itemInfo = new SpecialCostomItemInfo();
        itemInfo.setInfoId(specialCostomInfo.getId());
        itemInfo.setStatus(1);
        // 查询机构的任务信息
        List<SpecialCostomItemInfo> costomItemInfoList = specialCostomItemInfoService.selectSpecialCostomItemInfoList(itemInfo);
        mmap.put("costomItemInfoList", costomItemInfoList);

        mmap.put("loanAmount", SpecialEnum.BaseTypeInfo.loanAmount);
        mmap.put("zhimaScore", SpecialEnum.BaseTypeInfo.zhimaScore);

        mmap.put("gjj", SpecialEnum.BaseTypeInfo.gjj);
        mmap.put("house", SpecialEnum.BaseTypeInfo.house);
        mmap.put("car", SpecialEnum.BaseTypeInfo.car);
        mmap.put("sbaodan", SpecialEnum.BaseTypeInfo.baodan);
        mmap.put("shebao", SpecialEnum.BaseTypeInfo.shebao);

        return prefix + "/manualPush";
    }

    @RequiresPermissions("special:info:manualPush")
    @PostMapping("/manualPushAction")
    @ResponseBody
    public AjaxResult manualPushAction(@RequestParam("uid") String uid,
                                       @RequestParam("infoId") String infoId) {
        return marketApiService.specialPushData(uid, infoId);
    }

}
